package com.dearbinge.opentapi.content.address;

import com.dearbinge.data.api.address.AddressService;
import com.dearbinge.data.pojo.address.Regions;
import com.dearbinge.data.pojo.address.UserAddress;
import com.dearbinge.utils.ResultMsg;
import com.dearbinge.utils.enums.ResultStatusCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收货地址控制层
 *
 * @author wes
 * @date 2019/1/10
 */
@Api(tags = "3.1、用户-收货地址")
@RestController
@RequestMapping("/address")
@CrossOrigin
@EnableAutoConfiguration
public class AddressController {

    @Autowired
    private AddressService addressService;
    /**
     * 增加修改收货地址
     *
     * @return 结果提示
     * @date 18:56 2019/1/4
     */
    @ApiOperation(value = "增加修改收收货地址", notes = "增加修改收货地址")
    @RequestMapping(value = "/insertOrUpdateAddress", method = RequestMethod.POST)
    public ResultMsg<String> insertOrUpdateAddress(@RequestBody UserAddress userAddress) {
        try {
            Integer dataCode ;
            if(userAddress.getId() == null || userAddress.getId() ==0 ){
                //判断该用户的地址数
              Integer total = addressService.countAddressByUserId(userAddress.getUserId());
                 int maxAddress = 20;
                if(total > maxAddress){
                    ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(),"最大地址只能添加20个");
                }
                dataCode = addressService.insertAddress(userAddress);
                if (dataCode >0){
                    return ResultMsg.ok("增加成功");
                }
            }else {
                dataCode = addressService.updateAddress(userAddress);
                if (dataCode >0){
                    return ResultMsg.ok("修改成功");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), ResultStatusCodeEnum.CODE500.getDesc());
    }

    /**
     * 根据ID删除
     */
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query",required = true)
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultMsg<String> delete(Integer id) {
        try {
            Integer dataCode = addressService.deleteAddress(id);
            if (dataCode > 0){
                return ResultMsg.ok("删除成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据id查询地址
     */
    @ApiOperation(value = "根据id查询地址",notes = "根据id查询地址")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query",required = true)
    @RequestMapping(value = "/getAddressById",method = RequestMethod.GET)
    public ResultMsg<UserAddress> getAddressById(Integer id) {
        try {
            UserAddress userAddress = addressService.getAddressById(id);
            return ResultMsg.ok(userAddress);

        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 根据用户查询收货地址集
     */
    @ApiOperation(value = "根据用户查询收货地址集",notes = "根据用户查询收货地址集")
    @ApiImplicitParam(name = "userId", value = "userId", paramType = "query",required = true)
    @RequestMapping(value = "/listAddressByUserId",method = RequestMethod.GET)
    public ResultMsg< List<UserAddress>> listAddressByUserId(String userId) {
        try {
            List<UserAddress> list = addressService.listAddressByUserId(userId);
            return ResultMsg.ok(list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }


    /**
     * 省市联动
     */
    @ApiOperation(value = "省市联动",notes = "省市联动")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query")
    @RequestMapping(value = "/listProvinceAndCity",method = RequestMethod.GET)
    public ResultMsg< List<Regions>> listProvinceAndCity(String id) {
        try {
            List<Regions> list = addressService.listProvinceAndCity(id);
            return ResultMsg.ok(list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

}
